package com.example.backend.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.backend.entity.Account;
import com.example.backend.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Component
public class AuthUtil {
    
    @Autowired
    private AccountMapper accountMapper;
    
    @Autowired
    private WeBASEUtils weBASEUtils;
    
    /**
     * 通用登录方法
     * @param accountId 账户ID
     * @param password 密码
     * @param accountType 账户类型
     * @return 登录结果信息
     */
    public String login(String accountId, String password, Integer accountType) {
        // 根据accountId和accountType查询用户
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        queryWrapper.eq("account_type", accountType);
        
        Account account = accountMapper.selectOne(queryWrapper);
        
        // 判断用户是否存在
        if (account == null) {
            return "用户不存在";
        }
        
        // 检查账号状态
        if (account.getStatus() == 0) {
            return "账号正在审核中，请耐心等待";
        }
        
        // 验证密码
        if (!account.getPassword().equals(password)) {
            return "密码错误";
        }
        
        return "登录成功";
    }
    
    /**
     * 通用注册方法
     * @param username 用户名
     * @param passwordF 第一次输入的密码
     * @param passwordS 第二次输入的密码
     * @param qualificationContent 资质内容
     * @param accountType 账户类型
     * @return 注册结果信息
     */
    public String register(String username, String passwordF, String passwordS, String qualificationContent, int accountType) {
        if (!passwordS.equals(passwordF)) {
            return "密码不一致";
        }
    
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", username);
        Account account = accountMapper.selectOne(queryWrapper);
        if (account != null) {
            return "用户已存在";
        }
        
        account = new Account();
        account.setAccountId(username);
        account.setPassword(passwordS);
        account.setQualificationProof(0);
        account.setQualificationContent(qualificationContent);
        account.setAccountType(accountType);
        account.setCreatedAt(LocalDateTime.now());
        account.setUpdatedAt(LocalDateTime.now());
        account.setStatus(0); // 注册时默认状态为不正常
        accountMapper.insert(account);
        

        return "注册成功";
    }
    
    /**
     * 修改密码方法
     * @param accountId 账户ID
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @param accountType 账户类型
     * @return 修改结果信息
     */
    public String changePassword(String accountId, String oldPassword, String newPassword, int accountType) {
        // 根据accountId查询用户
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        queryWrapper.eq("account_type", accountType);
        Account account = accountMapper.selectOne(queryWrapper);

        // 验证用户是否存在
        if (account == null) {
            return "用户不存在";
        }

        // 验证旧密码
        if (!account.getPassword().equals(oldPassword)) {
            return "旧密码错误";
        }

        // 修改密码
        account.setPassword(newPassword);
        account.setUpdatedAt(LocalDateTime.now());
        accountMapper.updateById(account);
        return "修改密码成功";
    }
    
    /**
     * 提交资质方法
     * @param accountId 账户ID
     * @param qualificationContent 资质内容
     * @param accountType 账户类型
     * @return 提交结果信息
     */
    public String postQualificationContent(String accountId, String qualificationContent, int accountType) {
        // 根据accountId和accountType查询用户
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId);
        queryWrapper.eq("account_type", accountType);
        
        Account account = accountMapper.selectOne(queryWrapper);
        
        // 判断用户是否存在
        if (account == null) {
            return "用户不存在";
        }
        
        // 更新资质内容
        account.setQualificationContent(qualificationContent);
        account.setQualificationProof(1);
        account.setUpdatedAt(LocalDateTime.now());
        accountMapper.updateById(account);
        return "提交资质成功";
    }


    /**
     * 生成ID
     * 种类+当前时间yyyyMMddHHmmss+0~999循环数
     *
     * @return id
     */
    //自动生成上架记录ID（liID）
    public String generatelistID() {
        return "li" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + (int)(Math.random() * 1000);
    }


    //自动生成订单id（orId）
    public String generateOrderId() {
        return "or" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + (int)(Math.random() * 1000);
    }

    //自动生成商品制造id（goId）
    public String generateGoodsId() {
        return "go" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + (int)(Math.random() * 1000);
    }
}